//
//  DYTimePickerView.m
//  kafuMerchant
//
//  Created by 帝云科技 on 2018/9/13.
//  Copyright © 2018年 帝云科技. All rights reserved.
//

#import "DYTimePickerView.h"

@interface DYTimePickerView ()

@property (nonatomic,strong)UIToolbar *toolBar;
@property (nonatomic,copy)NSString *seletedTime;


@end

@implementation DYTimePickerView

-(instancetype)initWithFrame:(CGRect)frame
{
    self=[super initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    if (self) {
        [self initView];
    }
    return self;
}
-(void)initView
{
    
    self.backgroundColor=[UIColor colorWithWhite:0 alpha:0.5];
    self.toolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0,kScreenHeight,kScreenWidth,45)];
    self.toolBar.barStyle=UIBarStyleDefault;
    self.toolBar.barTintColor=[UIColor xhq_base];
    [self addSubview:self.toolBar];
    
    self.titleButton = [[UIBarButtonItem alloc] initWithTitle:@"请选择时间" style: UIBarButtonItemStylePlain target: nil action: nil];
    self.titleButton.tintColor=WhiteColor;
    [self.titleButton setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont boldSystemFontOfSize:18],NSFontAttributeName, nil] forState:UIControlStateNormal];
    
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"确认  " style: UIBarButtonItemStyleDone target: self action: @selector(done)];
    rightButton.tintColor=WhiteColor;
    
    UIBarButtonItem *leftButton  = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:@selector(cancel)];
    UIBarButtonItem *fixedButton  = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target: nil action: nil];
    NSArray *array = [[NSArray alloc] initWithObjects: leftButton,fixedButton, self.titleButton,fixedButton, rightButton, nil];
    
    
    [self.toolBar setItems:array];
    [self addSubview:self.timePicker];

    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
    tap.delegate=self;
    [self addGestureRecognizer:tap];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if ([touch.view isDescendantOfView:self.toolBar]||[touch.view isDescendantOfView:self.timePicker]) {
        return NO;
    }
    return  YES;
}
-(void)tap
{
    [UIView animateWithDuration:0.5f animations:^{
        self.timePicker.frame=CGRectMake(0,self.xhq_height, kScreenWidth,self.timePicker.xhq_height);
        self.toolBar.frame=CGRectMake(0, self.xhq_height, kScreenWidth, self.toolBar.xhq_height);
        
    } completion:^(BOOL finished) {
        self.hidden=YES;
        [self removeFromSuperview];
    }];
}
-(void)show
{
    [[[UIApplication sharedApplication] keyWindow] addSubview:self];
    [UIView animateWithDuration:0.5f animations:^{
        self.toolBar.frame=CGRectMake(0,self.xhq_height-(216+45),kScreenWidth,45);
        self.timePicker.frame=CGRectMake(0,self.xhq_height-216, kScreenWidth,216);
        self.hidden=NO;
    }];
}
-(void)done
{
    if (self.timePickerBlock) {
        self.timePickerBlock(self.seletedTime);
    }
    
    [UIView animateWithDuration:0.5f animations:^{
        self.timePicker.frame=CGRectMake(0,self.xhq_height, kScreenWidth,self.timePicker.xhq_height);
        self.toolBar.frame=CGRectMake(0, self.xhq_height, kScreenWidth, self.toolBar.xhq_height);
        
    } completion:^(BOOL finished) {
        self.hidden=YES;
        [self removeFromSuperview];
    }];
}

-(void)setToolbarTitle:(NSString *)toolbarTitle
{
    _toolbarTitle=toolbarTitle;
    
    [self.titleButton  setTitle:toolbarTitle];
}

-(UIDatePicker *)timePicker
{
    if (!_timePicker) {
        _timePicker=[[UIDatePicker alloc]initWithFrame:CGRectMake(0, kScreenHeight,kScreenWidth,BILIHEIGHT(250))];
        _timePicker.maximumDate=[NSDate date];
        _timePicker.datePickerMode=UIDatePickerModeDate;
        _timePicker.backgroundColor=WhiteColor;
        NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];//设置为中
        _timePicker.locale = locale;
        [_timePicker addTarget:self action:@selector(selectedDatePicker:) forControlEvents:UIControlEventValueChanged];
        
        self.seletedTime=[Utils timestampSwitchTime:[Utils getNowTimestamp] andFormatter:@"yyyy-MM-dd"];
//        NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
//        //        formatter.dateFormat=@"HH:mm";
//        formatter.dateFormat=@"yyyy-MM-dd";
//        timeStr=[formatter stringFromDate:_timePicker.date];
//        accurateTimeStr=[formatter stringFromDate:_timePicker.date];
        
    }
    return _timePicker;
}
-(void)selectedDatePicker:(UIDatePicker *)timePicker
{
    NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
    formatter.dateFormat=@"yyyy-MM-dd";
    self.seletedTime=[formatter stringFromDate:timePicker.date];
    
    if (self.timePickerBlock) {
        self.timePickerBlock(self.seletedTime);
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
